# This is a sample Python script.

# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

temp_path = '/Users/wanggh/Desktop/bbb.png'
src_path = '/Users/wanggh/Desktop/test.jpg'
img = cv.imread(src_path, 0)
img2 = img.copy()
template = cv.imread(temp_path, 0)
w, h = template.shape[::-1]
# 列表中所有的6种比较方法
methods = ['cv.TM_CCOEFF', 'cv.TM_CCOEFF_NORMED', 'cv.TM_CCORR',
           'cv.TM_CCORR_NORMED', 'cv.TM_SQDIFF', 'cv.TM_SQDIFF_NORMED']
img = img2.copy()
method = eval('cv.TM_SQDIFF_NORMED')
# 应用模板匹配
res = cv.matchTemplate(img, template, method)
min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)
# 如果方法是TM_SQDIFF或TM_SQDIFF_NORMED，则取最小值
if method in [cv.TM_SQDIFF, cv.TM_SQDIFF_NORMED]:
    top_left = min_loc
else:
    top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
cv.rectangle(img, top_left, bottom_right, (255, 0, 0), 2)
cv.imshow('ORB_Algorithm', img)
# cv.imshow("test", src)
cv.waitKey(0)
cv.destroyAllWindows()
